package com.vilen.weixin.service;

import com.vilen.basic.util.JsonUtil;
import com.vilen.weixin.kit.WeixinBasicKit;
import com.vilen.weixin.model.WeixinFinalValue;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * Created by Vilen on 2016/3/11.
 */
@Service("wqrService")
public class WqrService implements IWqrService {

    @Override
    public String loadTicketByBaseQr(int snum) {
        try {
            String url = WeixinBasicKit.replaceAccessTokenUrl(WeixinFinalValue.QR_GET);
            String json = "{\"action_name\": \"QR_LIMIT_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": "+snum+"}}}";
            String rjson = WeixinBasicKit.sendJsonPost(url, json);
            if (WeixinBasicKit.checkRequestSucc(json)) {
                return JsonUtil.getMapper().readTree(rjson).get("ticket").asText();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String loadTicketByTempQr(int snum) {
        try {
            String url = WeixinBasicKit.replaceAccessTokenUrl(WeixinFinalValue.QR_GET);
            String json = "{\"expire_seconds\": 180, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": "+snum+"}}}";
            String rjson = WeixinBasicKit.sendJsonPost(url, json);
            if (WeixinBasicKit.checkRequestSucc(json)) {
                return JsonUtil.getMapper().readTree(rjson).get("ticket").asText();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
